package cn.information.dao;


import cn.information.bean.Gysxx;
import cn.information.utils.DButil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class GysxxDao {
    public List<Gysxx> findAll() {
        Connection conn = DButil.getConnection ();
        String sql = "select * from Community_info";
        Statement stmt = null;
        List<Gysxx> list =new ArrayList<>();
        try {
            stmt =conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            while(rs.next()){
                Gysxx gysxx = new Gysxx();
                gysxx.setGysid(rs.getInt("gysid"));
                gysxx.setGysname(rs.getString("gysname"));
                gysxx.setGysadress(rs.getString("gysadress"));
                gysxx.setGysphone(rs.getString("gysphone"));
                list.add(gysxx);
            }
        } catch(Exception e){
            e.printStackTrace();
        }
        finally{
            DButil.close(conn);
        }
        return list;
    }


    public boolean addCommunity(Gysxx gysxx) {
        Connection conn = DButil.getConnection ();
        String sql = "insert into Community_info(district_id,district_name,address,floor_space) values(?,?,?,?)";
        PreparedStatement stmt = null;//try catch异常处理
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, gysxx.getGysid());
            stmt.setString(2, gysxx.getGysname());
            stmt.setString(3, gysxx.getGysadress());
            stmt.setString(4, gysxx.getGysphone());
            if(stmt.executeUpdate()>0){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DButil.close(conn);
        }
        return false;
    }

    public boolean deleteCommunity(int id) {
        Connection conn = DButil.getConnection ();
        String sql = "delete from Community_info where district_id=?";
        PreparedStatement stmt = null;
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, id);
            if(stmt.executeUpdate()>0){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DButil.close(conn);
        }
        return false;
    }

    public boolean updateCommunity(Gysxx gysxx) {
        Connection conn = DButil.getConnection ();
        String sql = "update Community_info set district_name=?,address=?,floor_space=? where district_id=?";
        PreparedStatement stmt = null;
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, gysxx.getGysname());
            stmt.setString(2, gysxx.getGysadress());
            stmt.setString(3, gysxx.getGysphone());
            stmt.setInt(4, gysxx.getGysid());
            if(
                    stmt.executeUpdate()>0){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DButil.close(conn);
        }
        return false;
    }
}
